package com.greenant.store.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.greenant.store.R;
import com.greenant.store.service.RetrieveDataAsyn;
import com.greenant.store.service.RetrieveDataListener;
import com.greenant.store.utils.UtilTools;
import com.lsarah.utils.SystemBarTintManager;

public class DescFuncActivity extends BaseActivity
{
	private Button mBtnBack;
	private TextView mTvCommit;
	private EditText mEtContent;
	private EditText mEtEmail;
	
	private boolean isCommitting = false;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        SystemBarTintManager.setContentView(this, R.layout.activity_descfunc, getResources().getColor(R.color.firebrick));
        mTvCommit = (TextView)findViewById(R.id.evaluation_feedback_top_tv_edit);
		mEtContent = (EditText)findViewById(R.id.evaluation_feedback_et);
		mEtEmail = (EditText)findViewById(R.id.evaluation_feedback_et_email);
		
		mTvCommit.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				feedBack(v);	
			}
		});
    }
    public void goBack(View view) {
        finish();
    }
    public void feedBack(View view) {
    	

		if(isCommitting) {
			UtilTools.showMyToast(DescFuncActivity.this, 
					getResources().getString(R.string.evaluation_feedback_commiting));
			return;
		}
		
		String content = mEtContent.getText().toString();
		if(TextUtils.isEmpty(content)) {
			UtilTools.showMyToast(DescFuncActivity.this, 
					getResources().getString(R.string.evaluation_feedback_no_content));
			return;
		}
		
		String email = mEtEmail.getText().toString();
		email = (email==null ? "" : email);
		if(!TextUtils.isEmpty(email)) {
			if(!UtilTools.isEmail(email)) {
				UtilTools.showMyToast(DescFuncActivity.this, 
						getResources().getString(R.string.evaluation_feedback_email_format_error));
				return;
			}
		}
		
		isCommitting = true;
		String evaluationFeedback = content + "(Email:" + email + ")";
		Log.d("CEXX", "CEXX ---> EvaluationFeedbackActivity ---> evaluationFeedback : " + evaluationFeedback);
		RetrieveDataAsyn.getInstance().commentFeedback(
				AVUser.getCurrentUser().getObjectId(), 
				evaluationFeedback, 
				-1, new RetrieveDataListener() {
					@Override
					public void dataRetrieved(int requestId, Object data) {
						isCommitting = false;
						if(data != null) {
							UtilTools.showMyToast(DescFuncActivity.this, 
									getResources().getString(R.string.evaluation_feedback_committed));
							DescFuncActivity.this.finish();
						}
						else {
							UtilTools.showMyToast(DescFuncActivity.this, 
									getResources().getString(R.string.evaluation_feedback_committed_failed));
						}
					}
				});
	
    }

    
}
